package com.ws.zn.user.controller;

import com.ws.zn.api.user.UserService;
import com.ws.zn.mapper.user.entity.SysUser;
import com.ws.zn.mapper.user.entity.SysUserInfo;
import com.ws.zn.mapper.user.param.RegisterUser;
import com.ws.zn.mapper.user.param.UpdateUser;
import com.ws.zn.mapper.user.param.UpdateUserInfo;
import com.ws.rain.base.result.R;
import com.ws.rain.base.utils.encrypt.Base64Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "UserController", description = "用户管理")
@RefreshScope
public class UserController {
    @Value("${spring.user.name}")
    private String name;
    private UserService userService;

    @Autowired
    public UserController(UserService userService){
           this.userService = userService;
    }

    @PostMapping("/register")
    @ApiOperation("注册用户")
    public R register(RegisterUser user) {
       SysUser sysUser = userService.register(user);
        return R.data(sysUser);
    }

    @PostMapping("/update")
    @ApiOperation("修改用户个人信息")
    public R updateUserInfo(UpdateUserInfo user) {
       SysUserInfo sysUser = userService.updateUserInfo(user);
        return R.data(sysUser);
    }

    @PostMapping("/update/pwd")
    @ApiOperation("修改用户密码")
    public R updatePwd(UpdateUser user) {
        String str = userService.updateUser(user);
        return R.data(str);
    }

    @GetMapping("/success")
    @ApiOperation("登录成功，回调接口")
    public R loginSuccess(String username) {
        String temp = Base64Util.decode(username);
        SysUser user = userService.loginSuccess(temp);
        return R.data(user);
    }

    @PostMapping("/update/role")
    @ApiOperation("修改用户角色")
    public R updateRole(UpdateUser user) {
        String str = userService.updateUser(user);
        return R.data(str);
    }

    @GetMapping("/name")
    @ApiOperation("获取名称")
    public R getName() {
        return R.data(name);
    }

}
